home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group98c.txt / 000139_icon-group-sender _Thu Dec 17 12:32:43 1998.msg < prev    next >
Internet Message Format  |  2000-09-20  |  1KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id MAA17561
  4.     for icon-group-addresses; Thu, 17 Dec 1998 12:32:26 -0700 (MST)
  5. Message-Id: <199812171932.MAA17561@baskerville.CS.Arizona.EDU>
  6. Date: Thu, 17 Dec 1998 11:07:13 -0800
  7. From: kwalker@sfo.harbinger.com (Ken Walker)
  8. To: icon-group@optima.CS.Arizona.EDU
  9. Subject: Re: Small Icon programming problem
  10. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  11. Status: RO
  12.  
  13. Here is a rather straightforward solution to the problem of
  14. sorting digits while preserving the sign of an integer.
  15.  
  16. There should be a solution that is shorter and more obscure; I
  17. thought of trying csets, but they discard duplicate digits...
  18.  
  19. Ken Walker, kenneth.walker@sfo.harbinger.com
  20. Harbinger Coporation, Concord, Ca. 94520
  21.  
  22.  
  23. procedure disort(i)
  24.    local digitLst, sortedInt;
  25.  
  26.    #
  27.    # Separate the integer into a sign and a list of digits.
  28.    #
  29.    digitLst := [];
  30.    i ? {
  31.       sortedInt := tab(any('-+')) | ""
  32.       while put(digitLst, move(1));
  33.    }
  34.  
  35.    #
  36.    # Sort the digits and append them to the output.
  37.    #
  38.    digitLst := sort(digitLst);
  39.    while sortedInt ||:= get(digitLst);
  40.  
  41.    return integer(sortedInt);
  42. end
  43.  
  44. (You can tell I've been programming too much in C by all the
  45. semicolons at the end of lines; it also took me a few tries
  46. to get all the colons before the equals. :-)
  47.